// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Subsystems/GameInstanceSubsystem.h"
#include "ScreenShotSubsystem.generated.h"

class UTexture2D;

UENUM(BlueprintType)
enum class EScreenShotType : uint8
{
	ESST_Normal UMETA(DisplayName = "普通截图(不带UI)"),
	ESST_NormalWithUI UMETA(DisplayName = "普通截图(带UI)"),
	ESST_HighResShot UMETA(DisplayName = "高精度截图(不带UI)")
};

UENUM(BlueprintType)
enum class EScreenShotSuffix : uint8
{
	ESSS_PNG UMETA(DisplayName = "PNG"),
	ESSS_JPG UMETA(DisplayName = "JPG")
};

UENUM(BlueprintType)
enum class EFixType : uint8
{
	EFT_Prefix UMETA(DisplayName = "前缀"),
	EFT_Suffix UMETA(DisplayName = "后缀")
};

USTRUCT(BlueprintType)
struct FResolution
{
	GENERATED_BODY()
	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "ScreenSize")
	int32 Width{0};
	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "ScreenSize")
	int32 Height{0};
};

USTRUCT(BlueprintType)
struct FScreenShotParams
{
	GENERATED_BODY()
	UPROPERTY(BlueprintReadWrite, Category = "SSParams")
	EScreenShotType ScreenShotType {EScreenShotType::ESST_Normal};
	UPROPERTY(BlueprintReadWrite, Category = "SSParams")
	EScreenShotSuffix SSSuffix {EScreenShotSuffix::ESSS_PNG};
	UPROPERTY(BlueprintReadWrite, Category = "SSParams")
	FString Filename {FString()};
	UPROPERTY(BlueprintReadWrite, Category = "SSParams")
	FString SavedFilePath {FString()};
	UPROPERTY(BlueprintReadWrite, Category = "SSParams", meta=(EditConditionHides = "ScreenShotType=EScreenShotType::ESST_HighResShot"))
	FResolution Resolution {FResolution()};
	
	FString ReturnFilePath {FString()};
};

/**
 * 
 */
UCLASS()
class SCREENSHOT_API UScreenShotSubsystem : public UGameInstanceSubsystem
{
	GENERATED_BODY()
public:
	virtual void Initialize(FSubsystemCollectionBase& Collection) override;
	virtual void Deinitialize() override;
	virtual bool ShouldCreateSubsystem(UObject* Outer) const override;
	
	static FDelegateHandle DelegateHandle;
	
	// 从本地路径导入图片并转换为Texture2D格式
	UFUNCTION(BlueprintCallable, Category = "LoadAssets")
	UTexture2D* LoadTexture2DFromLocal(UPARAM(ref) FString& ImagePath, const bool bAbsPath = false) const;
	// 从二进制文件转换为Texture2D格式
	UFUNCTION(BlueprintCallable, Category = "LoadAssets")
	UTexture2D* CreateTexture2DFromBytes(UPARAM(ref) TArray<uint8>& ImageData) const;

	// 创建二维码
	UFUNCTION(BlueprintPure, Category = "LoadAsserts")
	UTexture2D* CreateQRCodeToTexture2D(const FString& URL, int32 PixelSize = 8, FColor BackColor = FColor::White, FColor ForeColor = FColor::Black) const;

	// 实现截图
	UFUNCTION(BlueprintCallable, Category = "ScreenShot")
	FString CaptureImage(UPARAM(ref) FScreenShotParams& SSParams);
	// 获取同意文件夹下的某前缀或后缀的文件
	UFUNCTION(BlueprintCallable, Category = "Tools")
	TArray<FString> GetLocalAllFiles(UPARAM(ref) FString& FilePath, const FString FileFix, const EFixType FixType = EFixType::EFT_Suffix);
	UFUNCTION(BlueprintCallable, Category = "Tools")
	void DeleteFolder(const FString FilePath);
	// Base64URL相关
	UFUNCTION(BlueprintPure, Category = "Base64URL")
	FString Base64URLEncode(const FString& InString);
	UFUNCTION(BlueprintPure, Category = "Base64URL")
	FString Base64URLDecode(const FString& InString);
private:
	static void OnScreenshotCompleteImage(int32 InWidth, int32 InHeight, const TArray<FColor>& InColors, const FScreenShotParams SSParams);
};
